package com.peitamerchants.application;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.multidex.MultiDex;

import com.facebook.react.ReactApplication;
import com.eguma.barcodescanner.BarcodeScannerPackage;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactRootView;
import com.facebook.react.common.LifecycleState;
import com.igexin.sdk.PushManager;
import com.peitamerchants.BuildConfig;
import com.peitamerchants.Common.CommonPackage;
import com.peitamerchants.R;
import com.peitamerchants.service.DemoIntentService;
import com.peitamerchants.service.DemoPushService;
import com.yoloci.fileupload.FileUploadPackage;
import com.imagepicker.ImagePickerPackage;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import com.zachary.reactnative.baidumap.BaiduMapPackage;
import com.reactnative.ivpusic.imagepicker.PickerPackage;
import com.theweflex.react.WeChatPackage;

import cn.reactnative.modules.qq.QQPackage;


import java.util.Arrays;
import java.util.List;

import io.rong.imkit.RongIM;

public class MainApplication extends Application implements ReactApplication {

    private static MainApplication app;
    private  static final CommonPackage commonPackage = new CommonPackage();
    private static ReactInstanceManager mReactInstanceManager;
    private static String token = null;
    private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
        @Override
        public boolean getUseDeveloperSupport() {
            return BuildConfig.DEBUG;
        }

        @Override
        protected List<ReactPackage> getPackages() {
            return Arrays.<ReactPackage>asList(
                    new MainReactPackage(),
                    new BarcodeScannerPackage(),
                    new FileUploadPackage(),
                    new ImagePickerPackage(),
                    new PickerPackage(),
                    new WeChatPackage(),
                    new QQPackage(),
                    new BaiduMapPackage(getApplicationContext()),
                    commonPackage
            );
        }
    };

    @Override
    public ReactNativeHost getReactNativeHost() {
        return mReactNativeHost;
    }

    public static CommonPackage getCommonPackage(){
        return commonPackage;
    }
    public static void getmReactInstanceManager(Bundle bundle, ReactRootView reactRootView) {

        if (mReactInstanceManager == null) {
            mReactInstanceManager = ReactInstanceManager.builder()
                    .setApplication(app)
                    .setBundleAssetName("index.android.bundle")
                    .setJSMainModuleName("index.android")
                    .addPackage(new MainReactPackage())
                    .setUseDeveloperSupport(com.facebook.fbcore.BuildConfig.DEBUG)
                    .setInitialLifecycleState(LifecycleState.RESUMED)
                    .build();
        }
        if (reactRootView != null) {
            reactRootView.startReactApplication(mReactInstanceManager, "PeitaMerchants", bundle);
        }

    }

    public static String getTokenUrl() {
        return "?access-token=" + token;
    }

    public static void setToken(String token){
        MainApplication.token = token;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        init();




        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                StatusBarCompat.compat(activity, Color.parseColor("#4F91ED"));
            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
    }

    public static MainApplication getApp() {
        return app;
    }


    private void init() {
        app = this;
        SoLoader.init(this, /* native exopackage */ false);
        // 融云的初始化
        RongIM.init(this);
        PushManager.getInstance().initialize(this.getApplicationContext(), DemoPushService.class);
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), DemoIntentService.class);
    }

    /**
     * 分割 Dex 支持
     *
     * @param base
     */
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}
